//
//  ViewController.m
//  系统截屏
//
//  Created by mac on 16/6/20.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "TestViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *image;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor redColor];
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setBackgroundColor:[UIColor greenColor]];
    
    btn.frame =CGRectMake(100,100, 100, 30);
    [btn setTitle:@"点击截屏" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside ];

    [self.view addSubview:btn];
    
}

-(void)btn:(UIButton *)sender{
    
    TestViewController *testVC =[[TestViewController alloc]init];
    testVC.image = [self captureScreen];
    [self presentViewController:testVC animated:YES completion:nil];
}
- (UIImage *) captureScreen {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
